package com.one.group.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.*;
import com.one.group.enums.ErrorCode;
import com.one.group.exception.BusinessException;
import com.one.group.model.dto.SpHouseRentalAddDto;
import com.one.group.model.dto.SpHouseRentalQueryDto;
import com.one.group.model.dto.SpHouseRentalRentDto;
import com.one.group.model.dto.SpHouseRentalUpdateDto;
import com.one.group.model.entity.*;
import com.one.group.model.vo.SpHouseRentalListVo;
import com.one.group.service.SpHouseRentalService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@DubboService
@Service
public class SpHouseRentalServiceImpl extends ServiceImpl<SpHouseRentalMapper, SpHouseRental> implements SpHouseRentalService {
    @Override
    public boolean mySave(SpHouseRentalAddDto spHouseRentalAddDto) {
        try{
            SpHouseRental spHouseRental = new SpHouseRental();
            spHouseRental.setHouseId(spHouseRentalAddDto.getHouseId());
            spHouseRental.setOwnerId(spHouseRentalAddDto.getOwnerId());
            spHouseRental.setCellId(spHouseRentalAddDto.getCellId());
            spHouseRental.setAddress(spHouseRentalAddDto.getAddress());
            spHouseRental.setPrice(spHouseRentalAddDto.getPrice());
            spHouseRental.setType(spHouseRentalAddDto.getType());
            spHouseRental.setRemark(spHouseRentalAddDto.getRemark());
            spHouseRental.setStatus(1);
            return this.save(spHouseRental);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.ADD_ERROR.getCode(),"添加房屋租赁信息失败");
        }
    }

    @Override
    public boolean myUpdate(SpHouseRentalUpdateDto spHouseRentalUpdateDto) {
        try {
            SpHouseRental spHouseRental = this.getById(spHouseRentalUpdateDto.getId());
            if (spHouseRental != null){
                spHouseRental.setPrice(spHouseRentalUpdateDto.getPrice());
                spHouseRental.setType(spHouseRentalUpdateDto.getType());
                spHouseRental.setRemark(spHouseRentalUpdateDto.getRemark());
                return this.updateById(spHouseRental,true);
            }
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR.getCode(),"房屋租赁信息不存在");
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.UPDATE_ERROR.getCode(),"更新房屋租赁信息失败");
        }
    }
    @Autowired
    private SpHouseMapper spHouseMapper;
    @Autowired
    private UserAccountMapper userAccountMapper;
    @Autowired
    private SpRenterMapper spRenterMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean rentHouse(SpHouseRentalRentDto spHouseRentalRentalRentDto) {
        try{
            Long renterId = 0L;
            //创建租客
            UserAccount userAccount = userAccountMapper.selectOneById(spHouseRentalRentalRentDto.getRenterId());
            if(userAccount == null){
                throw new BusinessException(ErrorCode.NOT_FOUND_ERROR.getCode(),"租客不存在");
            }else {
                //构建租客信息
                SpRenter spRenter = new SpRenter();
                spRenter.setName(userAccount.getRealName());
                spRenter.setPhone(userAccount.getPhone());
                spRenter.setEmail(userAccount.getEmail());
                renterId = spRenterMapper.myInsert(spRenter);
            }
            // 更新房屋租赁信息
            SpHouseRental spHouseRental = this.getById(spHouseRentalRentalRentDto.getId());
            if (spHouseRental != null){
                spHouseRental.setStatus(2);
                if(this.updateById(spHouseRental)){
                    SpHouse spHouse = spHouseMapper.selectOneById(spHouseRental.getHouseId());
                    spHouse.setRenterId(renterId);
                    return spHouseMapper.update(spHouse) == 1;
                }else {
                    throw new RuntimeException("更新房屋租赁信息失败");
                }
            } throw new RuntimeException("房屋租赁信息不存在");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<SpHouseRentalListVo> listSpHouseRentals(SpHouseRentalQueryDto spHouseRentalQueryDto) {
        //设置租户id
        if (spHouseRentalQueryDto.getTenantId() == null || spHouseRentalQueryDto.getTenantId()==0){
            Long tenantId = Convert.toLong(StpUtil.getExtra("tenant_id"));
            spHouseRentalQueryDto.setTenantId(tenantId);
        }
        return getMapper().listSpHouseRentals(spHouseRentalQueryDto);
    }

    @Override
    public SpHouseRental myGetById(Long houseId) {
        QueryWrapper query = QueryWrapper.create().from(SpHouseRental.class)
                .eq(SpHouseRental::getHouseId, houseId);
        return this.getOne(query);
    }
    @Autowired
    private SpCellMapper spCellMapper;
    @Override
    public String queryHouseRentInfo(Long cellId) {
        //根据小区id查询小区名与租户id
        SpCell spCell = spCellMapper.selectOneById(cellId);
        SpHouseRentalQueryDto spHouseRentalQueryDto = new SpHouseRentalQueryDto();
        spHouseRentalQueryDto.setName(spCell.getName());
        spHouseRentalQueryDto.setTenantId(spCell.getTenantId());
        List<SpHouseRentalListVo> list = listSpHouseRentals(spHouseRentalQueryDto);
        return list.toString();
    }
}
